package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaUtil;
import com.hemaapp.hm_FrameWork.chat.FirPagCount;
import com.hemaapp.hm_FrameWork.chat.FirPagDBClient;
import com.hemaapp.hm_FrameWork.dialog.HemaButtonDialog;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.chat.ChatPrivateActivity;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类描述：聊天消息列表适配器
 * 创建人：GuiyouZhang
 * 创建时间：2016/8/2 20:12
 * 修改备注：
 */
public class ChatListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private ArrayList<FirPagCount> datas;

    public ChatListAdapter(Context context, ArrayList<FirPagCount> datas) {
        this.mContext = context;
        this.datas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.listitem_empty_base, parent, false);
            return new EmptyViewHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_notice, parent, false);
            return new ViewHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (datas.size() == 0) {
            if (holder instanceof EmptyViewHolder)
                ((EmptyViewHolder) holder).tvEmtpy.setText("暂无消息记录");
        } else {
            if (holder instanceof ViewHolder) {
                ((ViewHolder) holder).setData(datas.get(position));
            }
        }
    }

    @Override
    public int getItemCount() {
        int size = datas == null ? 0 : datas.size();
        return size == 0 ? 1 : datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.size() == 0)
            return -1;
        else
            return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.avatar)
        ImageView ivAvatar;
        @Bind(R.id.point)
        ImageView point;
        @Bind(R.id.nickname)
        TextView nickname;
        @Bind(R.id.date)
        TextView date;
        @Bind(R.id.content)
        TextView content;

        FirPagCount data;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (data != null) {
                        Intent itChat = new Intent(mContext, ChatPrivateActivity.class);
                        itChat.putExtra("to_client_id", data.getdxclientid());
                        itChat.putExtra("to_nickname", data.getdxclientname());
                        itChat.putExtra("to_avatar", data.getdxclientavatar());
                        mContext.startActivity(itChat);
                    }
                }
            });
            view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (data != null) {
                        HemaButtonDialog dialog = new HemaButtonDialog(mContext);
                        dialog.setText("确定要删除该信息？");
                        dialog.setLeftButtonText("取消");
                        dialog.setRightButtonText("确定");
                        dialog.setButtonListener(new HemaButtonDialog.OnButtonListener() {

                            @Override
                            public void onRightButtonClick(HemaButtonDialog dialog) {
                                dialog.cancel();
                                FirPagDBClient client = FirPagDBClient.get(mContext);
                                client.delete(data);
                                datas.remove(data);
                                notifyDataSetChanged();
                            }

                            @Override
                            public void onLeftButtonClick(HemaButtonDialog dialog) {
                                dialog.cancel();
                            }
                        });
                    }
                    return false;
                }
            }
            );
        }

        public void setData(FirPagCount data) {
            this.data = data;
            ImageUtils.loadAvatar(mContext, data.getdxclientavatar(), ivAvatar);
            nickname.setText(data.getdxclientname());
            date.setText(data.gettime());
            if (data.getDxpacktype().equals("1")) {//（1：文本 2：图片 3：音频 4：视频）
                //显示帖子内容，并将其中emoji表情进行转换
                HemaUtil.SetMessageTextView(mContext, content, data.getcontent());
            }else if (data.getDxpacktype().equals("2")) {
                content.setText("[图片]");
            }else if (data.getDxpacktype().equals("3")) {
                content.setText("[语音]");
            }else if (data.getDxpacktype().equals("4")) {
                content.setText("[视频]");
            }
            if ("0".equals(data.getcount())) {
                point.setVisibility(View.GONE);
            } else {
                point.setVisibility(View.VISIBLE);
            }
        }
    }
}
